<?php
class Db_news extends CI_Model{
	public function __construct(){
		parent::__construct();
	}
	
	public function news_item_list(){
		$config['base_url'] = base_url().'manage_news/news_list';
		if($this->session->userdata("sess_customer_name") != "eshine"){
			$this->db->where("customer_pkid",$this->session->userdata("sess_customer_id"));
		}
		$config['total_rows'] = $this->db->count_all_results("mobile_activity");
		$config['per_page'] = 20;
		$config['full_tag_open'] = '<div class="pages">';
		$config['full_tag_close'] = '<div>';
		$this->pagination->initialize($config);
		$this->db->select('t1.pkid,t1.title_en,t2.name');
		$this->db->from('mobile_activity t1');
		$this->db->join("mobile_customer t2", "t1.customer_pkid=t2.pkid",'left');
		if($this->session->userdata("sess_customer_name") != "eshine")
			$this->db->where("t2.pkid",$this->session->userdata("sess_customer_id"));
		//$this->db->limit($config['per_page'],$this->uri->segment(3));
		$this->db->order_by("t2.pkid", "desc");
		$res = $this->db->get();
		//==
		$no = $this->uri->segment(3)+1;
		$i = 1;
		$arr = $res->result_array();
		$item = array();
		foreach ($arr as $P){
			if($i %2 == 0)
				$tmp['back']							="#FFFFFF";
			else
				$tmp['back']							="#F4F4F4";
			$i++;
			$tmp['no']							=$no;
			$tmp['pkid']						=$P['pkid'];
			$tmp['title_en']					=$P['title_en'];
			$tmp['name']						=$P['name'];
			$item[] = $tmp;
			$no = $no+1;
		}
		$num_row = $config['total_rows'];
		$pagelink  = $this->pagination->create_links();
		return array($item,$num_row,$pagelink,$config['total_rows']);
	}
	
	public function gallery_item_list(){
		$config['base_url'] = base_url().'manage_gallery/gallery_list';
		if($this->session->userdata("sess_customer_name") != "eshine"){
			$this->db->where("pkid",$this->session->userdata("sess_customer_id"));
		}
		$config['total_rows'] = $this->db->count_all_results("mobile_customer");
		$config['per_page'] = 20;
		$config['full_tag_open'] = '<div class="pages">';
		$config['full_tag_close'] = '<div>';
		$this->pagination->initialize($config);
		if($this->session->userdata("sess_customer_name") != "eshine")
			$this->db->where("pkid",$this->session->userdata("sess_customer_id"));
		//$this->db->limit($config['per_page'],$this->uri->segment(3));
		$this->db->order_by("pkid", "desc");
		$res = $this->db->get("mobile_customer");
		//==
		$no = $this->uri->segment(3)+1;
		$i = 1;
		$arr = $res->result_array();
		$item = array();
		foreach ($arr as $P){
			if($i %2 == 0)
				$tmp['back']							="#FFFFFF";
			else
				$tmp['back']							="#F4F4F4";
			$i++;
			$tmp['no']							=$no;
			$tmp['pkid']						=$P['pkid'];
			$tmp['name']						=$P['name'];
			$tmp['c_count']					=number_format($this->gallery_count($P['pkid']));
			$item[] = $tmp;
			$no = $no+1;
		}
		$num_row = $config['total_rows'];
		$pagelink  = $this->pagination->create_links();
		return array($item,$num_row,$pagelink,$config['total_rows']);
	}
	
	public function gallery_count($cid){
		if($cid != null){
			$this->db->where("customer_pkid",$cid);
			$num = $this->db->count_all_results("mobile_gallery");
			return $num;
		}
	}
	
	public function vdo_item_list(){
		$config['base_url'] = base_url().'manage_vdo/vdo_list';
		if($this->session->userdata("sess_customer_name") != "eshine")
			$this->db->where("customer_pkid",$this->session->userdata("sess_customer_id"));
		$config['total_rows'] = $this->db->count_all_results("mobile_video");
		$config['per_page'] = 20;
		$config['full_tag_open'] = '<div class="pages">';
		$config['full_tag_close'] = '<div>';
		$this->pagination->initialize($config);
		$this->db->select('t1.pkid,t1.name_en,t2.name');
		$this->db->from('mobile_video t1');
		$this->db->join("mobile_customer t2", "t1.customer_pkid=t2.pkid",'left');
		if($this->session->userdata("sess_customer_name") != "eshine")
			$this->db->where("t2.pkid",$this->session->userdata("sess_customer_id"));
		//$this->db->limit($config['per_page'],$this->uri->segment(3));
		$this->db->order_by("t2.pkid", "desc");
		$res = $this->db->get();
		//==
		$no = $this->uri->segment(3)+1;
		$i = 1;
		$arr = $res->result_array();
		$item = array();
		foreach ($arr as $P){
			if($i %2 == 0)
				$tmp['back']							="#FFFFFF";
			else
				$tmp['back']							="#F4F4F4";
			$i++;
			$tmp['no']							=$no;
			$tmp['pkid']						=$P['pkid'];
			$tmp['title_en']					=$P['name_en'];
			$tmp['name']						=$P['name'];
			$item[] = $tmp;
			$no = $no+1;
		}
		$num_row = $config['total_rows'];
		$pagelink  = $this->pagination->create_links();
		return array($item,$num_row,$pagelink,$config['total_rows']);
	}
	
	public function customer_list($id = ""){
		$str = '<select name="customer" id="select">';
		$str .='<option value="">=select customer=</option>';
		$this->db->order_by("pkid","desc");
		if($this->session->userdata("sess_customer_name") != "eshine")
			$this->db->where("pkid",$this->session->userdata("sess_customer_id"));
		$row = $this->db->get("mobile_customer")->result_array();
		foreach ($row as $P){
			if($P['pkid'] == $id) $check  = 'selected="selected"';
			else $check  = '"';
			$str .='<option value="'.$P['pkid'].'" '.$check.'>'.$P['name'].'</option>';
		}
		$str .="</select>";
		return $str;
	}
	
	public function activity_type($id=""){
		$str = "";
		if($id == 1) $s1 = 'selected="selected"';
			else $s1="";
		if($id==2) $s2=  'selected="selected"';
			else $s2="";
		if($id == 3) $s3=  'selected="selected"';
			else $s3 = "";
		$str .='<option value="1" '.$s1.'>News</option>';
		$str .='<option value="2" '.$s2.'>Event</option>';
		$str .='<option value="3" '.$s3.'>Promotion</option>';
		return $str;
	}
	
}
?>